package net.sahv.bdyz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sahv.bdyz.model.Aa;
import net.sahv.bdyz.service.AaService;
import net.sahv.bdyz.util.PagingUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("admin/aa")
public class AaController {
	
	@Autowired
	private AaService aaService;
	
	@RequestMapping(value = "/aaList", method = RequestMethod.GET)
	public String aaList() {
		return "admin/aa/aaList";
	}
	
	@ResponseBody
	@RequestMapping(value="/aaList",method=RequestMethod.POST)
	public JSON aaList(PagingUtil pagingUtil,HttpServletRequest request,Model model){
		
		//获取查询参数
		String A_NAME=request.getParameter("A_NAME");
		
		String sortField=" ";
		String where="";
		
		where+=" WHERE 1=1 ";
		
		if(A_NAME!=null&&!A_NAME.trim().equals("")){
			where+=" AND A_NAME LIKE '%"+A_NAME+"%' ";
		}
		
		Map<String,Object>  param = new HashMap<String,Object>();
		
		param.put("start", pagingUtil.getiDisplayStart());
		param.put("length", pagingUtil.getiDisplayLength());
		param.put("where", where);
		param.put("sortField", sortField);
		
		List<Map<String, Object>> aaList = aaService.aaList(param);
		//统计信息的总条数
		Integer aaListCount=aaService.aaListCount(where);
		
		Map<String,Object>  map = new HashMap<String,Object>();
		map.put("aaData", aaList);
		//查出来总共有多少条记录
		map.put("iTotalRecords", aaListCount);
		map.put("iTotalDisplayRecords", aaListCount);
		map.put("sEcho", pagingUtil.getsEcho());
		 
		return (JSON) JSON.toJSON(map);
	}
	
	@RequestMapping(value="/insertAa",method=RequestMethod.GET)
	public String insertAa(){
		return "admin/aa/insertAa";
	}
	
	@ResponseBody
	@RequestMapping(value="/insertAa",method=RequestMethod.POST)
	public ApiResult insertAa(Aa aa){
		ApiResult result=new ApiResult();
		int flag=aaService.insert(aa);
		if(flag==1){
			result.msg.status=0;
			result.msg.desc="添加成功！";
		}else{
			result.msg.status=1;
			result.msg.desc="添加失败！";
		}
		return result;
	}
	
	@RequestMapping(value="/updateAa",method=RequestMethod.GET)
	public String updateAa(Model model,Integer id){
		Aa aa=aaService.selectByPrimaryKey(id);
		model.addAttribute("aa", aa);
		return "admin/aa/updateAa";
	}
	
	@ResponseBody
	@RequestMapping(value="/updateAa",method=RequestMethod.POST)
	public ApiResult updateAa(Aa aa){
		ApiResult result=new ApiResult();
		Aa aa2=aaService.selectByPrimaryKey(aa.getaId());
		aa2.setaId(aa.getaId());
		aa2.setaName(aa.getaName());
		aa2.setaSex(aa.getaSex());
		aa2.setaCreatetime(aa.getaCreatetime());
		aa2.setaIsDelete(aa.getaIsDelete());
		int flag=aaService.updateByPrimaryKey(aa2);
		if(flag==1){
			result.msg.status=0;
			result.msg.desc="修改成功！";
		}else{
			result.msg.status=1;
			result.msg.desc="修改失败！";
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteAa",method=RequestMethod.POST)
	public ApiResult deleteAa(Integer[] id){
		ApiResult result=new ApiResult();
		int flag=0;
		for(int i:id){
			flag=aaService.deleteByPrimaryKey(i);
			if(flag==0){
				result.msg.status=1;
				result.msg.desc="删除失败！";
			}
		}
		if(flag==1){
			result.msg.status=0;
			result.msg.desc="删除成功！";
		}
		return result;
	}
	
}
